package com.peter.ultimatebar

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.peter.ultimatebar.bean.BarConfig
import com.peter.ultimatebar.core.*
import com.peter.ultimatebar.core.addObserver
import com.peter.ultimatebar.core.defaultNavigationBar
import com.peter.ultimatebar.core.ultimateBarXInitialization
import com.peter.ultimatebar.core.updateStatusBar
import com.peter.ultimatebar.extension.setStatusBarSystemUiFlagWithLight
import com.peter.ultimatebar.extension.setSystemUiFlagWithLight

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun FragmentActivity.applyStatusBar(config: BarConfig) {
    ultimateBarXInitialization()
    val navLight = manager.getNavigationBarConfig(this).light
    setSystemUiFlagWithLight(config.light, navLight)
    updateStatusBar(config)
    defaultNavigationBar()
    addObserver()
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun FragmentActivity.applyNavigationBar(config: BarConfig) {
    ultimateBarXInitialization()
    val staLight = manager.getStatusBarConfig(this).light
    setSystemUiFlagWithLight(staLight, config.light)
    updateNavigationBar(config)
    defaultStatusBar()
    addObserver()
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun Fragment.applyStatusBar(config: BarConfig) {
    requireActivity().ultimateBarXInitialization()
    ultimateBarXInitialization()
    val navLight = manager.getNavigationBarConfig(this).light
    requireActivity().setSystemUiFlagWithLight(config.light, navLight)
    updateStatusBar(config)
    requireActivity().defaultNavigationBar()
    addObserver()
    requireActivity().addObserver()
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun Fragment.applyNavigationBar(config: BarConfig) {
    requireActivity().ultimateBarXInitialization()
    ultimateBarXInitialization()
    val staLight = manager.getStatusBarConfig(this).light
    requireActivity().setSystemUiFlagWithLight(staLight, config.light)
    updateNavigationBar(config)
    requireActivity().defaultStatusBar()
    addObserver()
    requireActivity().addObserver()
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun FragmentActivity.applyStatusBarOnly(config: BarConfig) {
    statusBarOnlyInitialization()
    setStatusBarSystemUiFlagWithLight(config.light)
    updateStatusBar(config)
    addObserver(true)
}

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun Fragment.applyStatusBarOnly(config: BarConfig) {
    requireActivity().statusBarOnlyInitialization()
    statusBarOnlyInitialization()
    requireActivity().setStatusBarSystemUiFlagWithLight(config.light)
    updateStatusBar(config)
    addObserver(true)
    requireActivity().addObserver(true)
}